package com.blog.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import com.blog.admin.bean.Menu;
import com.blog.admin.bean.Role;
import com.blog.admin.dao.MenuMapper;
import com.blog.admin.dao.RoleMapper;
import com.blog.admin.service.MenuService;
import com.mysql.fabric.xmlrpc.base.Array;

@Service
public class MenuServiceImpl implements MenuService
{
	@Resource
	MenuMapper menuMapper;
	
	@Resource
	RoleMapper roleMapper;
	
	public List<Menu> qeuryAllMenu() 
	{
		return menuMapper.selectAllMenu();
	}

	public List<Menu> queryMenuByRoleList(List<Role> roleList) {
		List<Menu> listMenuOne = new ArrayList<Menu>();
		List<Menu> listMenuTwo = new ArrayList<Menu>();
		for(Role role:roleList)
		{
			//获取到一级菜单
			for(Menu menu:menuMapper.selectMenuOneByRole(role))
			{
				if(!roleExist(listMenuOne,menu.getId()))
				{
					//不重复插入
					listMenuOne.add(menu);
				}
			}
			//获取到二级菜单
			for(Menu menu:menuMapper.selectMenuTwoByRole(role))
			{
				if(!roleExist(listMenuTwo,menu.getId()))
				{
					//不重复插入
					listMenuTwo.add(menu);
				}
			}
		}
		//建立菜单对应关系
		
		for(Menu meunOne:listMenuOne)
		{
			List<Menu> lsitChildMenu = new ArrayList<Menu>();
			for(Menu meunTwo:listMenuTwo)
			{
				if(meunOne.getId()==meunTwo.getParentId())
				{
					lsitChildMenu.add(meunTwo);
				}
			}
			meunOne.setChildrenMenuList(lsitChildMenu);

		}

		
		return listMenuOne;
	}
	
	public List<Menu> qeuryParentMenu() {
		List<Menu> listMeun = menuMapper.selectParentMenu();
		for(Menu m :listMeun)
		{
			m.setChildrenMenuList(menuMapper.selectChildrenMenuByMenu(m));
		}
		return listMeun;
	}
	
	/**
	 * @todo 检验是否重复
	 * @author fg
	 * @date 2016年1月21日
	 */
	public Boolean roleExist (List<Menu> oldRoleList,Integer menuId)
	{
		for(Menu menu:oldRoleList)
		{
			if(menuId == menu.getId())
			{
				return true;
			}
		}
		return false;
	}

	public Integer addMenu(Menu menu) {
		if(0==menu.getParentId())
		{
			menu.setMenuUrl(null);
		}
		return menuMapper.insertMenu(menu);
	}

	public Menu queryMenuByMenuId(Integer id) {
		return menuMapper.selectMenuByMenuId(id);
	}

	public Integer modifyMenu(Menu menu) {
		return menuMapper.updateMenu(menu);
	}

	public JSONArray queryMenuByRoleId(Integer id,Boolean isModify) {
		JSONArray jsonArray = new JSONArray();
		List<Menu> allMenuList = menuMapper.selectAllMenu();
		List<Menu> roleMenuList = new ArrayList<Menu>();
		if(isModify)
		{
		   roleMenuList = menuMapper.selectAllMenuByRoleId(id);
		}
		for(Menu menu:allMenuList)
		{
			JSONObject json = new JSONObject();
			json.put("id", menu.getId());
			json.put("parentId", menu.getParentId());
			json.put("name", menu.getMenuName());
			if(isModify && existInRoleMenuList(menu.getId(),roleMenuList))
			{
				json.put("checked", true);
			}
			jsonArray.add(json);
		}
		return jsonArray;
	}
	
	public Boolean existInRoleMenuList(Integer id,List<Menu> roleMenuList)
	{
		for(Menu menu:roleMenuList)
		{
			if(menu.getId()==id)
			{
				return true;
			}
		}
		return false;
	}

	public Integer removeMenuById(Integer id) 
	{
		return menuMapper.deleteMenuById(id);
	}
	
	
}
